package cn.itcast.mobilebrowser.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.itcast.mobilebrowser.dao.BookMarkDao;
import cn.itcast.mobilebrowser.database.BrowserDatabase;
import cn.itcast.mobilebrowser.model.BookMark;

public class BookMarkViewModel extends AndroidViewModel {

    private final LiveData<List<BookMark>> allBookMarks;

    private final BookMarkDao bookMarkDao;

    public BookMarkViewModel(@NonNull @NotNull Application application) {
        super(application);
        bookMarkDao = BrowserDatabase.getDataBase(application).getBookMarkDao();
        allBookMarks = bookMarkDao.getAllBookMarks();
    }

    public LiveData<List<BookMark>> getAllBookMarks() {
        return allBookMarks;
    }

    public LiveData<List<BookMark>> searchBookMarks(String webTitle) {
        return bookMarkDao.getBookMarksByTitle(webTitle);
    }

    public void insert(BookMark bookMark) {
        BrowserDatabase.databaseWriteExecutor.execute(() -> bookMarkDao.insert(bookMark));
    }

    public void delete(BookMark bookMark) {
        BrowserDatabase.databaseWriteExecutor.execute(() -> bookMarkDao.delete(bookMark));
    }

    public void update(BookMark bookMark) {
        BrowserDatabase.databaseWriteExecutor.execute(() -> bookMarkDao.update(bookMark));
    }
}
